package com.fastwire.carriere.inventory.entity;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

import org.tmforum.mtop.fmw.xsd.nam.v1.RelativeDistinguishNameType;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.entity.BaseObject;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.EntityUtil;

// TODO: Auto-generated Javadoc
/**
 * The Class NetworkEntity.
 */
public abstract class NetworkEntity extends BaseObject implements Serializable {
	
	/** The oid. */
	private long oid; // identifier
	
	/** The version. */
	private int version;
	
	/** The name. */
	protected String name;
	
	/** The ems native name. */
	protected String emsNativeName;

	/** The status. */
	protected String status;
	
	/** The discovery date. */
	protected Date discoveryDate;
	
	/** The invalidate date. */
	protected Date invalidateDate;

	/**
	 * Gets the version.
	 *
	 * @return the version
	 */
	public int getVersion() {
		return version;
	}

	/**
	 * Sets the version.
	 *
	 * @param version the new version
	 */
	public void setVersion(int version) {
		this.version = version;
	}

	/**
	 * Gets the oid.
	 *
	 * @return the oid
	 */
	public long getOid() {
		return oid;
	}

	/**
	 * Sets the oid.
	 *
	 * @param oid the new oid
	 */
	public void setOid(long oid) {
		this.oid = oid;
	}

	/**
	 * Gets the invalidate date.
	 *
	 * @return the invalidate date
	 */
	public Date getInvalidateDate() {
		return invalidateDate;
	}

	/**
	 * Sets the invalidate date.
	 *
	 * @param invalidateDate the new invalidate date
	 */
	public void setInvalidateDate(Date invalidateDate) {
		this.invalidateDate = invalidateDate;
	}

	/**
	 * Gets the discovery date.
	 *
	 * @return the discovery date
	 */
	public Date getDiscoveryDate() {
		return discoveryDate;
	}

	/**
	 * Sets the discovery date.
	 *
	 * @param discoveryDate the new discovery date
	 */
	public void setDiscoveryDate(Date discoveryDate) {
		this.discoveryDate = discoveryDate;
	}

	// end

	/**
	 * Instantiates a new network entity.
	 */
	public NetworkEntity() {
		// setStartDate(new Date());
		setDiscoveryDate(new Date());
		setStatus(AppConstants.NE_STATUS_ACTIVE);
	}

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;

	}

	/**
	 * Gets the ems native name.
	 *
	 * @return the ems native name
	 */
	public String getEmsNativeName() {

		return emsNativeName;
	}

	/**
	 * Sets the ems native name.
	 *
	 * @param emsNativeName the new ems native name
	 */
	public void setEmsNativeName(String emsNativeName) {
		this.emsNativeName = emsNativeName;
	}

	/**
	 * Save to csv.
	 *
	 * @param writer the writer
	 */
	public void saveToCSV(FileWriter writer) {

		try {

			writer.append("\n");
			Class currentClass = this.getClass();

			do {

				Field[] fields = currentClass.getDeclaredFields();
				CommonLogger.debug(this,
						"current class is" + currentClass.getName()
								+ ", it has " + fields.length + " attributs");
				for (int index = 0; index < fields.length; index++) {
					fields[index].setAccessible(true);
					if (fields[index].getType().equals(String.class)) {

						writer.append((String) fields[index].get(this));
						writer.append(",");

					}
				}

				currentClass = currentClass.getSuperclass();
			}

			while (currentClass != null);
		} catch (IllegalArgumentException e) {
			CommonLogger.error(this, e.getMessage());
		} catch (IOException e) {
			CommonLogger.error(this, e.getMessage());
		} catch (IllegalAccessException e) {
			CommonLogger.error(this, e.getMessage());
		}

	}

	/**
	 * Sets the name.
	 *
	 * @param rdnList the new name
	 */
	public void setName(List<RelativeDistinguishNameType> rdnList) {

		this.name = EntityUtil.convertRtnListToString(rdnList);
	}

	/**
	 * Gets the status.
	 *
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * Sets the status.
	 *
	 * @param status the new status
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * Convert rtn list to string.
	 *
	 * @param rdnList the rdn list
	 * @return the string
	 */
	public String convertRtnListToString(
			List<RelativeDistinguishNameType> rdnList) {

		StringBuffer stringBuffer = new StringBuffer();

		for (RelativeDistinguishNameType rdnType : rdnList) {
			stringBuffer.append(rdnType.getType());
			stringBuffer.append("=");
			stringBuffer.append(rdnType.getValue());
			stringBuffer.append("::");

		}

		return stringBuffer.toString();
	}

	/**
	 * Invalidate.
	 */
	public void invalidate() {

		setInvalidateDate(new Date());
		setStatus(AppConstants.NE_STATUS_INACTIVE);
	}

	/**
	 * Validate.
	 */
	public void validate() {
		// setEndDate(null);
		setStatus(AppConstants.NE_STATUS_ACTIVE);
	}

}
